<cfcomponent>
	
	<cffunction name="startOver" access="public" output="false" returntype="any">
		
		<cfset var local = {} />
		
		<cfquery datasource="tmeyer">
			TRUNCATE TABLE State
			TRUNCATE TABLE Move
		</cfquery>
		
		<cfset game = application.javaloader.create("State") />
		<cfset game.newGame() />
		
		<cfset local.StateID = application.db.getStateByBinary(
			game.serializeState(),
			game.isCheckmate(),
			game.isStalemate(),
			game.getTurn()
			).ID />

		<cfset application.db.saveMoves(local.stateID, game.getLegalMoves()) />
		
	</cffunction>

	<cffunction name="renderBoard" access="public" output="false" returntype="any">
		<cfargument name="stateID" required="true" />

		<cfset var local = {} />
		<cfset var game = "" />
		<cfset var state = "" />
		<cfset var moves = "" />
		<cfset var board = "" />
		<cfset var i = 0 />
		<cfset var j = 0 />

		<cfsavecontent variable="local.html">
			<cfset game = application.javaloader.create("State") />
			<cfset state = application.db.getStateByID(arguments.stateID) />
			
			<cfif State.recordCount eq 0>
				<cfset startOver() />
				<cfset state = application.db.getStateByID(arguments.stateID) />
			</cfif>
			
			<cfset game.init(state.state) />

			<cfif not state.moves_solved>
				<!--- Save moves to db --->
				<cfset application.db.saveMoves(stateID, game.getLegalMoves()) />
			</cfif>

			<cfoutput>
				<!--- Get Moves from db --->
				<cfset moves = application.db.getMovesByStateID(stateID) />
				<div class="information">
				<cfif game.isCheckmate()>
					Checkmate!
				<cfelseif game.isStalemate()>
					Stalemate!
				<cfelse>
					<cfif game.getTurn() eq "W">
						White's Turn
					<cfelse>
						Black's Turn
					</cfif>
					<cfif game.isCheck()>
						- Check!
					</cfif>
				</cfif>
				</div>
				<cfset board = game.getBoard() />
				<div id="table-setting">
				<table id="chess-board" cellpadding="0" cellspacing="0">

				<cfloop from="5" to="1" index="i" step="-1">
					<tr>
						<th>#i#</th>
					<cfloop from="1" to="5" index="j">
						<td>
						<cfif board.hasPiece(i-1,j-1)>
							<a class="">
							#this.getUnicodeCharacter(board.pieceAt(i-1,j-1).getColor(),board.pieceAt(i-1,j-1).getCharacter())#
							</a>
						<cfelse>
							&nbsp;
						</cfif>
						</td>
					</cfloop>
				</cfloop>
				<tr>
					<td>&nbsp;</td>
				<cfloop from="1" to="5" index="j">
					<th>#chr(j+96)#</th>
				</cfloop>
				</tr>
				</table>
				</div>
				<div class="valid-moves">
				<cfloop query="moves">
					<a href="board.cfm?stateID=#moves.new_state#">#moves.move#</a><br/>
				</cfloop>
				</div>
			</cfoutput>
		</cfsavecontent>

		<cfreturn trim(local.html) />

	</cffunction>

	<!------>

	<cffunction name="getUnicodeCharacter" access="public" output="false" returntype="string">
		<cfargument name="color" required="true" />
		<cfargument name="piece" required="true" />

		<cfset var local = {} />

		<cfset local.string = arguments.piece />
		
		<cfset arguments.piece = arguments.piece.toString() />

		<cfif arguments.color eq "B">
			<cfswitch expression="#arguments.piece#">
				<cfcase value="Q">
					<cfset local.string = "&##9819;" />
				</cfcase>
				<cfcase value="K">
					<cfset local.string = "&##9818;" />
				</cfcase>
				<cfcase value="R">
					<cfset local.string = "&##9820;" />
				</cfcase>
				<cfcase value="B">
					<cfset local.string = "&##9821;" />
				</cfcase>
				<cfcase value="N">
					<cfset local.string = "&##9822;" />
				</cfcase>
			    <cfcase value="P">
			       <cfset local.string = "&##9823;" />
			    </cfcase>
				<cfdefaultcase>
			        <cfset local.string = arguments.piece />
			    </cfdefaultcase>
			</cfswitch>

		<cfelseif arguments.color eq "W">

			<cfswitch expression="#arguments.piece#">
				<cfcase value="Q">
					<cfset local.string = "&##9813;" />
				</cfcase>
				<cfcase value="K">
					<cfset local.string = "&##9812;" />
				</cfcase>
				<cfcase value="R">
					<cfset local.string = "&##9814;" />
				</cfcase>
				<cfcase value="B">
					<cfset local.string = "&##9815;" />
				</cfcase>
				<cfcase value="N">
					<cfset local.string = "&##9816;" />
				</cfcase>
			    <cfcase value="P">
			       <cfset local.string = "&##9817;" />
			    </cfcase>
				<cfdefaultcase>
			        <cfset local.string = arguments.piece />
			    </cfdefaultcase>
		    </cfswitch>

		</cfif>

		<cfreturn local.string />

	</cffunction>

</cfcomponent>